/*
 *
 *
 *
 */
#include "InputManager.h"

#ifndef _INPUTMANAGER_CPP_
#define _INPUTMANAGER_CPP_

using namespace std;

/*
 * 
 */
InputManager::InputManager(void)
{
    int i;
    /*zera os vetores*/
    for(i = 0;i < KEY_SYM_MAX; i++)
    {
        keyState[PRESSED][i] = false;
        keyState[DOWN][i] = false;
        keyState[UP][i] = false;
    }
    for(i = 0;i < MOUSE_BUTTON_MAX; i++)
    {
        buttonState[PRESSED][i] = false;
        buttonState[DOWN][i] = false;
        buttonState[UP][i] = false;
    }
    eventQuit = false;
    posX = 0;
    posY = 0;
}

/*
 *
 */
InputManager::~InputManager(void)
{
    
}

/*
 * 
 */ 
void InputManager::poolEvents(void)
{
    SDL_Event event;
    int i;
    /*zera apenas os vetores keyDownState e keyUpState a cada frame*/
    for(i = 0;i < KEY_SYM_MAX; i++)
    {
        keyState[DOWN][i] = false;
        keyState[UP][i] = false;
    }
    for(i = 0;i < MOUSE_BUTTON_MAX; i++)
    {
        buttonState[DOWN][i] = false;
        buttonState[UP][i] = false;
    }
    while(SDL_PollEvent(&event)) 
    {
        /*eventos do teclado*/
        if( event.type == SDL_KEYDOWN )
        {
            keyState[DOWN][event.key.keysym.sym] = true;
            keyState[PRESSED][event.key.keysym.sym] = true;
        }
        if( event.type == SDL_KEYUP )
        {
            keyState[UP][event.key.keysym.sym] = true;
            keyState[PRESSED][event.key.keysym.sym] = false;
        }
        /*eventos do mouse*/
        if(event.type == SDL_MOUSEBUTTONDOWN)
        {
            buttonState[DOWN][event.button.button] = true;
            buttonState[PRESSED][event.button.button] = true;
        }
        if(event.type == SDL_MOUSEBUTTONUP)
        {
            buttonState[UP][event.button.button] = true;
            buttonState[PRESSED][event.button.button] = false;
        }
        if(event.type == SDL_MOUSEMOTION)
        {
            posX = event.motion.x;
            posY = event.motion.y;
        }
        /*outros eventos*/
        if( event.type == SDL_QUIT )
            eventQuit = true;
    }
}

/*
 *
 */
bool InputManager::isEventQuit(void)
{
    return eventQuit;
}

/*
 *
 */
bool InputManager::isKeyDown(int key)
{
    return keyState[DOWN][key];
}

/*
 *
 */
bool InputManager::isKeyUp(int key)
{
    return keyState[UP][key];
}

/*
 *
 */
bool InputManager::isKeyPressed(int key)
{
    return keyState[PRESSED][key];
}

/*
 *
 */
bool InputManager::isMouseDown(int key)
{
    return buttonState[DOWN][key];
}

/*
 *
 */
bool InputManager::isMouseUp(int key)
{
    return buttonState[UP][key];
}

/*
 *
 */
bool InputManager::isMousePressed(int key)
{
    return buttonState[PRESSED][key];
}

/*
 *
 */
Sint16 InputManager::mousePosX(void)
{
    return posX;
}

/*
 *
 */
Sint16 InputManager::mousePosY(void)
{
    return posY;
}

/*
 *
 */
bool InputManager::isMouseInside(Sprite *sprite)
{
    SDL_Rect spriteRect;
    spriteRect = sprite->getRect();
    /*testa se NÃo esta colidindo como Sprite*/
    if(((posX - spriteRect.x) > spriteRect.w) || ((posX - spriteRect.x) < 0))
        return false;
    else if(((posY - spriteRect.y) > spriteRect.h) || ((posY - spriteRect.y) < 0))
        return false;
    else
        return true;
}


#endif

